﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace HdipCricketClub.Models
{
    public class ClubFixturePlayerRepository
    {
        CricketClubEntities db = new CricketClubEntities();

        public IQueryable<ClubFixturePlayer> GetPlayers(int id)
        {
            return db.ClubFixturePlayers.Include("Fixture").Include("Member").Include("DismissalType")
                                        .Where(cfp => cfp.fixture_id == id)
                                        .OrderBy(cfp => cfp.Member.last_name);
        }

        public IQueryable<ClubFixturePlayer> GetClubBatsman(int id)
        {
            return db.ClubFixturePlayers.Include("Fixture").Include("Member").Include("DismissalType")
                                        .Where(cfp => cfp.fixture_id == id && cfp.batsman_number != null)
                                        .OrderBy(cfp => cfp.batsman_number);
        }

        public IQueryable<ClubFixturePlayer> GetClubBowlers(int id)
        {
            return db.ClubFixturePlayers.Include("Fixture").Include("Member").Include("DismissalType")
                                        .Where(cfp => cfp.fixture_id == id && cfp.bowler_number != null)
                                        .OrderBy(cfp => cfp.bowler_number);
        }

        public double TotalBatsmensRuns(int id)
        {
            var fixtureBatsmen = db.ClubFixturePlayers.Where(fb => fb.fixture_id == id);

            double totalBatsmenRuns = 0;

            foreach (var batsman in fixtureBatsmen)
            {
                if (batsman.batting_runs != null)
                {
                    totalBatsmenRuns += (double)batsman.batting_runs;
                }
            }
            return totalBatsmenRuns;
        }
    }
}